package classroom;

import java.io.*;
import java.text.DecimalFormat;

public class CopyFile extends Thread{

   // File src = new File("Day15\\QQMusic(1802.5318)20210630184644.logH.zip");
    //File dest = new File("D:\\QQMusicCache\\Log\\QQMusic\\QQMusic(1802.5318)20210630184644.logH.zip");
    // 源文件路径
    public File src;
    //复制目标路径
    public File dest;

    public CopyFile(String src, String dest) {
        this.src = new File(src);
        this.dest = new File(dest);
    }
    @Override
    public void run() {
        DecimalFormat df = new DecimalFormat("##%");
        long totalLength = src.length();//获取文件大小
        //高效字节，拷贝
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));

            byte [] arr = new byte[1024*8];
            //定义累加每次读到字节数量
            int sum_len =0;
            int len ;
            while ((len = bis.read(arr))!=-1){
                bos.write(arr,0,len);
                sum_len +=len ;
                String format = df.format(1.0 * sum_len / totalLength);
                System.out.println(src.getName()+"文件复制"+format);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
